<?php
class DealsController extends Controller
{
	public function init()
	{
		parent::init();
	}

	public function actionIndex()
	{
		//get params
		$intPage = Yii::app()->request->getParam('page', 1);
		$cateId =  Yii::app()->request->getParam('cateId', null);
		$intRowsPerPage = 20;
		//get deals
		$arrDeals = Posts::getInstance()->getList(array(
			'keyword' => null,
			'taxonomy_id' => 2,
			'post_status' => 1,	
			'post_parent' => null,
			'category_id' => $cateId,
			'page' => $intPage,
			'limit' => $intRowsPerPage,
		));
		$intTotalRows = isset($arrDeals[0]['total_rows'])?$arrDeals[0]['total_rows']:0;
		foreach($arrDeals as &$R){
			$R['post_category'] = Posts::getInstance()->getCategories($R['post_id']);
			$R['post_creator'] = Users::getInstance()->get($R['post_creator_id']);
		}
		//paging
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		//render layout
		$this->render('deals', array('arrDeals'=>$arrDeals));
	}
	
	public function actionDetail()
	{
		//get params
		$intPostId =  Yii::app()->request->getParam('postId', null);
		
		//detail
		$post = Posts::getInstance()->get($intPostId);
		
		//comments
		$comments = Comments::getInstance()->listComments($intPostId);
		
		//my post
		$arrMyPosts = Posts::getInstance()->getList(array(
			'taxonomy_id' => 2,
			'post_status' => 1,	
			'page' => 1,
			'limit' => 5,
		));
		
		//post same categories
		$arrOtherPosts = Posts::getInstance()->getList(array(
			'taxonomy_id' => 2,
			'post_status' => 1,	
			'page' => 1,
			'limit' => 5,
		));
		
		$this->render('detail', array(
			'post' => $post,
			'arrMyPosts' => $arrMyPosts,
			'arrOtherPosts' => $arrOtherPosts,
			'arrComments' => $comments
		));
	}
	
	public function actionUpload(){
		if(Yii::app()->request->isPostRequest)
		{
			try{
				$expiry = Yii::app()->request->getParam('expiry', null);
				//get params
				$args = array(
					'taxonomy_id' => 2,
					'post_title' => Yii::app()->request->getParam('title', null),
					'post_body' => Yii::app()->request->getParam('body', null),
					'post_expiry_date' => !empty($expiry)?DateTime::createFromFormat('d/m/Y', $expiry)->format('Y-m-d'):null,
					'post_creator_id' => Yii::app()->user->id,
					'post_status' => 1
				);
				//create deal
				$intPostId = Posts::getInstance()->insert($args);
									
				//update info
				if($intPostId > 0){
					//udpate category
					$arrCategoryIds = Yii::app()->request->getParam('category', null); 
					if(!empty($arrCategoryIds)){
						Posts::getInstance()->updateCategory($intPostId, $arrCategoryIds);
					}
					//update tags
					$strTags = Yii::app()->request->getParam('tags', null);
					if(!empty($strTags)){
						Posts::getInstance()->updateTags($intPostId, $strTags);
					}
					//update custom fields
					$arrFields = Yii::app()->request->getParam('fields', null);
					if(is_array($arrFields)){
						foreach($arrFields as $intFieldId => $strFieldValue){
							$strFieldValue = trim($strFieldValue);
							if(!empty($intFieldId)){
								Posts::getInstance()->updateField($intPostId, $intFieldId, $strFieldValue);
							}
						} 					
					}
					//update photo
					if(!empty($_FILES['images'])){
						$files = FileTransfer::rearrangeGlobalFilesArray($_FILES['images']);
						$slide = array();
						foreach($files as $file){
							if($file['error'] == UPLOAD_ERR_NO_FILE) continue;
							$objFileTransfer = new FileTransfer(UPLOADS_DIR, array('prefixPath'=>'src/'.date('Y').'/'.date('md'), 'allowedFileExtension'=>'jpg,jpeg,png,gif'));
							$arrFileInfo = $objFileTransfer->upload($file);
							if($arrFileInfo !== false){
								$args = array(
									'media_title' => $arrFileInfo['filename'],
									'media_file' => $arrFileInfo['path'],
									'media_file_size' => $arrFileInfo['size'],
									'media_file_extension' => $arrFileInfo['extension'],
									'media_src' => 1,
									'media_folder' => 'deal_'.$intPostId,
									'media_creator_id' => Yii::app()->user->id
								);
								$intResult = Media::getInstance()->insert($args);
								$slide[] = $arrFileInfo['path'];
								if($intResult > 0){
									//$this->thumb($arrFileInfo);
								}
							}
						}
						//update images slide
						Posts::getInstance()->updateField($intPostId, 6, implode(';', $slide));					
					}
				}				
				//done
				//$this->refresh();
				$this->redirect('/deals/'.Utils::uri_encode(Yii::app()->request->getParam('title', null)).'.'.$intPostId.'.html');
			}  catch(Exception $ex){
				o($ex->getMessage());
				Yii::app()->user->setFlash('error', $ex->getMessage());	
			}
		}
		if(Yii::app()->request->isAjaxRequest)
			echo $this->renderPartial('_upload');
		else
			$this->render('_upload');
	}
}